﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.DTOModel.Manager;
using Newtonsoft.Json;
using WPay.Application.Interface;
using WPay.Domain.Model.DTOs;
using WPay.Domain.Model.Entities.DTOs.Req;

namespace WPay.Web.API.Controllers
{
    /// <summary>
    /// 登录控制器
    /// </summary>
    public class LoginController : BaseController
    {

        public readonly IMngrBaseInfo_ApplicationService _mngrBaseInfo_ApplicationService;
        public LoginController(IMngrBaseInfo_ApplicationService mngrBaseInfo_ApplicationService)
        {
            _mngrBaseInfo_ApplicationService = mngrBaseInfo_ApplicationService;
        }

        /// <summary>
        /// 登陆接口
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public RespDto Login()
        {
            return Do<LoginDTO>((x, result) =>
              {
                  var loginUser = _mngrBaseInfo_ApplicationService.Login(x.txtName, x.txtPwd);
                  result.Data = new { Data = loginUser, Menu = JsonConvert.DeserializeObject<MenuDTO>(loginUser.Menu).menus };
              });
        }
        /// <summary>
        /// 验证是否登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public RespDto IsLogin()
        {
            return new RespDto().RunWithTry(x =>
            {
                x.Success = Function.IsLogin(HttpContext);

            });
        }
    }
}